<?php

/*
 * B端商品功能模块
 * 功能：
 */

namespace Home\Controller;

use Think\B_Controller;

class GoodsController extends B_Controller {
    
    public function __construct() {
        parent::__construct();
        $this->item = D('Shopitem','Logic');
    }
    
    /*商品列表*/
    public function index(){
        if(empty($this->store)){
            $this->_msg('请先创建店铺！');
        }
        $items = $this->item->getItemsByStoreIds($this->store_id);
        $items = array_chunk($items, 12);
        $this->assign('items',$items[0]);
        $this->display('goods');
    }
    
    public function create(){
        $id = (int)$_GET['id'];
        $store_id = (int)$_GET['store_id'];
        if($store_id && !in_array($store_id, $this->store_id)){
            $this->_msg('非法操作！');
        }
        if($id>0 && $store_id>0){
            $item = $this->item->getItemById($id,$store_id);
            if(!$item){
                $this->_msg('该商品不存在！');exit;
            }
            $item['intro'] = str_replace('<br/>', "\n",$item['intro']);
            $item['intro'] = str_replace('<span style=\'color:red\'>', "",$item['intro']);
            $item['intro'] = str_replace('</span>', "",$item['intro']);
            
        }
        if($_GET['copy']==1){
            $this->assign('copy',true);
        }
        $this->assign('item',$item);
        $this->display('create');
    }

    //根据store_id查询item单品
    public function item(){
        $store_id = $_GET['store_id'];

        if(!in_array($store_id, $this->store_id)){
            $this->_msg('非法操作！');
        }
        $items = $this->item->getItem($store_id);
        echo json_encode($items);exit;
    }
    
    public function submit(){
        $data['store_id'] = (int)$_POST['store_id'];
        if(!in_array($data['store_id'], $this->store_id)){
            $this->_msg('非法操作！');
        }
        $data['name'] = $_POST['name'];
        $data['title'] = $_POST['title'];
        $data['type'] = $_POST['type'];
        $data['is_meal_set'] = (int)$_POST['is_meal_set'];
        $data['price'] = (int)($_POST['price']*100);
        $data['intro'] = $_POST['intro'];
        $data['image'] = $_POST['image'];
        $data['create_time'] = time();
        $data['status'] = 2;
        //套餐
        if($data['is_meal_set']==1){
           $set = D('Shopmealset','Logic');
           $arr['num'] = $_POST['num'];
           $arr['items'] = $_POST['meal_set'];
           $arr['status']=1;
           $arr['store_id'] = $data['store_id'];
           $arr['create_time'] = $data['create_time'];
        }
        if((int)$_POST['member']==1){
            $data['level'] = json_encode($_POST['members']);
        }else{
            $data['level'] = 0 ;
        }
        if(strlen($data['name']) <0 ||
           count($data['image']) <1 ||
           strlen($data['title']) <1 ){
           $this->_msg('_SYSTEM_ERROR_');
        }
        $this->item->setStoreId($data['store_id']);
        if((int)$_POST['id']){
            // 更新
            $data['item_number'] = $_POST['item_number'];
            $getItem = $this->item->getItemByNumber($data['item_number'],$data['store_id']);
            $result =  array_intersect_assoc($getItem,$data);
            if(count($result) != 10 ){
                // 数据更改时 先删除后插入
                $biaoshi = 1;
            }
            foreach($data['image'] as $k=>$v){
                if($v != $getItem['images'][$k]['image']){
                    $biaoshi = 1;
                }
            }
             //更新套餐
            if($data['is_meal_set']==1){
                $arr['meal_set_number'] = $_POST['item_number'];
                $arr['meal_set_id'] = (int)$_POST['id'];
                $meal = $set->getMealSetsByMember($arr['meal_set_number'],$arr['meal_set_id'],$arr['store_id']);
                $_item = array();
                foreach($arr['items'] as $k=>$v){
                    if($v<1 || $arr['num'][$k]<1){
                        continue;
                    }
                    $_item[$k]['item_id'] = $v;
                    $_item[$k]['number'] = $arr['num'][$k];
                }
                if($biaoshi !=1){
                    foreach ($meal as $k => $v) {
                            $item[$k]['id'] = $v['item_id'];
                            $item[$k]['number'] = $v['number'];
                    }
                    if(count($_item)!=count($item)){
                        $biaoshi = 1;
                    }else{
                        foreach($_item as $k=>$v){
                            if($item[$k]['id']!=$v['item_id'] || $item[$k]['number']!=$v['number']){
                                $biaoshi = 1;//插入
                            }
                        }
                    }
                }
            }
            if($biaoshi == 1){
                //插入
                $id = $this->item->thingUpdateItem($data,$getItem,$meal,$_item);
                if($id===false){
                    $this->_msg('更新失败!');
                }
            }else{
                //更新
                redirect('/home/goods/preview?item_id='.$getItem['id'].'&store_id='.$data['store_id']);
            }
        }else{
            //添加
            $data['item_number'] = $this->number();
            $id = $this->item->thingInsertItem($data,$arr);
        }
        
                       
        if(!$id){
            $this->_msg('_SYSTEM_ERROR_');
        }else{
            if($_POST['caogao'] == "保存草稿"){
                redirect('/home/goods');
            }
        }
        redirect('/home/goods/preview?item_id='.$id.'&store_id='.$data['store_id']);
    }

     //item_number
    function number() {
        return 'P'. date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    public function preview(){
        $id = $_GET['item_id'];
        $store_id = (int)$_GET['store_id'];
        if(!in_array($store_id, $this->store_id)){
            $this->_msg('非法操作！');
        }
        if($id>0 && $store_id>0){
            $item = $this->item->getItemById($id,$store_id);
            if(!$item){
               $this->_msg('该商品不存在！');
            }
        }
        $this->assign('item',$item);
        $this->display('preview');
    }
    public function detail(){
        $id = (int)$_GET['id'];
        $store_id = (int)$_GET['store_id'];
        if(!in_array($store_id, $this->store_id)){
            $this->_msg('非法操作！');
        }
       if($id>0 && $store_id>0){    
            $item = $this->item->getItemById($id,$store_id,true);
            if(!$item){
               $this->_msg('该商品不存在！');
            }
            $item['intro'] = str_replace('<br/>', "\n",$item['intro']);
            $item['intro'] = str_replace('<span style=\'color:red\'>', "",$item['intro']);
            $item['intro'] = str_replace('</span>', "",$item['intro']);
            

        }
        if($item){
            $s = D('Store','Logic');
            $store = $s->getStore($store_id);
            $this->assign('store',$store);
        }
        $this->assign('item',$item);
        $this->display('detail');
    }
    
    public function info(){
        
    }

    public function soldOut(){
        $id = (int)$_GET['id'];
        $store_id = (int)$_GET['store_id'];
        if(!in_array($store_id, $this->store_id)){
            $this->_msg('非法操作！');
        }
        $re = $this->item->soldOut($id,$store_id);
        if($re){
            echo 1;
        }else{
            echo 0;
        }
        exit;

    }

    public function delete(){
        $id = (int)$_GET['id'];
        $store_id = (int)$_GET['store_id'];
        if(!in_array($store_id, $this->store_id)){
            $this->_msg('非法操作！');
        }
        $re = $this->item->deleteItem($id,$store_id);
        if($re){
            echo 1;
        }else{
            echo 0;
        }
        exit;
    }

    public function release(){
       $id = (int)$_GET['id'];
       $store_id = (int)$_GET['store_id'];
       if(!in_array($store_id, $this->store_id)){
            $this->_msg('非法操作！');
        }
       $re = $this->item->putAway($id,$store_id);
       if($re){
            redirect('/home/goods');
       }else{
            $this->_msg('_SYSTEM_ERROR_');
       } 
    }

    public function search(){
        $store_id = $_GET['store_id'];
        if($store_id && !in_array($store_id, $this->store_id)){
            $this->_msg('非法操作！');
        }
        $type = $_GET['type'];
        $status = $_GET['status'];
        $start = $_GET['start'];
        if($store_id==0){
            $items = array();
            foreach($this->store_id as $v){
                $re = $this->item->search($v,0,$status,$type);
                foreach($re as $v){
                    $items[] = $v;
                }
            }
        }else{
            $re = $this->item->search($store_id,$start,$status,$type);
            $items = $re;
        }
        $items = array_chunk($items, 12);
        $_start = ceil($start/12);
        if($start != 0 ){
            $_start = (int)$_start;
            if($store_id==0){
                $items = $items[$_start];
            }else{
                $items = $items[$_start-1];
            }
            if($items==false){
                exit('false');
            }

            echo json_encode($items);exit;
        }
        $this->assign('items',$items[0]);
        $this->assign('store_id',$store_id);
        $this->assign('type',$type);
        $this->assign('status',$status);
        $this->display('goods'); 
    } 

    public function getMealSetByMember(){
        $meal_set_number=$_GET['item_number'];
        $id = (int)$_GET['id'];
        $store_id = (int)$_GET['store_id'];
        if(!in_array($store_id,$this->store_id)){
            $this->_msg('该店铺非法！');
        }
        $re = $this->item->getItemByMealSetId($id,$store_id,true);
        $re = array_reverse($re);
        echo json_encode($re);
        exit;
    }

}